<template>
  <div class="field-layout">
	<div class="head">
		<div>
			<svg aria-hidden="true" class="svg-icon arrow" @click="handleExpand">
				<use v-if="!isExpand" href="#icon-right_arrow_normal"></use>
				<use v-else href="#icon-below_arrow_normal"></use> 
			</svg>
			<span>{{props.name}}</span>
		</div>
	</div>
	<div v-if="isExpand" class="body">
		<div  v-for=" field in styles" >
			<Vector2DField v-if="field.dataType.type=='v2' && field.display==true " :pidentifier="pidentifier" :identifier="field.identifier" :label="field.name" :value="field.dataType.value" :required="field.required"  @onchange="onchange"  />
			<EnumField v-if="field.dataType.type=='enum' && field.display==true " :pidentifier="pidentifier" :identifier="field.identifier" :label="field.name" :options="field.dataType.specs" :value="field.dataType.value" :required="field.required"  @onchange="onchange"  />
			<!-- <ImgField v-if="field.dataType.type=='img' && field.display==true " :baseUrl="baseUrl"  :pidentifier="pidentifier" :identifier="field.identifier" :label="field.name" :value="field.dataType.value" :required="field.required"  @onchange="onchange"  /> -->
			<Vector4DField v-if="field.dataType.type=='v4' && field.display==true " :pidentifier="pidentifier" :identifier="field.identifier" :label="field.name" :value="field.dataType.value" :required="field.required"  @onchange="onchange"  />
			<BooleanField v-if="field.dataType.type=='boolean' && field.display==true " :pidentifier="pidentifier" :identifier="field.identifier" :label="field.name" :options="field.dataType.specs" :value="field.dataType.value" :required="field.required"  @onchange="onchange"  />
			<ColorField v-if="field.dataType.type=='color' && field.display==true " :pidentifier="pidentifier"  :identifier="field.identifier" :label="field.name" :value="field.dataType.value" :required="field.required" @onchange="onchange" />
			<StringField v-if="field.dataType.type=='string' && field.display==true " :pidentifier="pidentifier" :identifier="field.identifier" :label="field.name" :length="field.dataType.specs.length" :value="field.dataType.value" :required="field.required" :readonly="field.readonly" @onchange="onchange" />
			<NumberField v-if="field.dataType.type=='float' && field.display==true " :pidentifier="pidentifier" :identifier="field.identifier" :label="field.name" :min="field.dataType.specs.min" :max="field.dataType.specs.max" :step="field.dataType.specs.step" :value="field.dataType.value" :required="field.required" @onchange="onchange"/>
			<NumberField v-if="field.dataType.type=='int' && field.display==true " :pidentifier="pidentifier" :identifier="field.identifier" :label="field.name" :min="field.dataType.specs.min" :max="field.dataType.specs.max" :step="field.dataType.specs.step" :value="field.dataType.value" :required="field.required" @onchange="onchange"/>
			<NumberField v-if="field.dataType.type=='double' && field.display==true " :pidentifier="pidentifier" :identifier="field.identifier" :label="field.name" :min="field.dataType.specs.min" :max="field.dataType.specs.max" :step="field.dataType.specs.step" :value="field.dataType.value" :required="field.required" @onchange="onchange"/>
			<StructField v-if="field.dataType.type=='struct' && field.display==true" :baseUrl="baseUrl" :pidentifier="pidentifier+'.'+field.identifier" :list="field.dataType.specs" :name="field.name"  @onchange="onchange"/>
		</div>
	</div>
  </div>
</template>
 
<script lang="ts" setup>
	import EnumField from "@/components/FormField/EnumField.vue";
	// import ImgField from "@/components/FormField/ImgField.vue";
	import Vector2DField from "@/components/FormField/Vector2DField.vue";
	import Vector4DField from "@/components/FormField/Vector4DField.vue";
	import BooleanField from "@/components/FormField/BooleanField.vue";
	import ColorField from "@/components/FormField/ColorField.vue";
	import StringField from "@/components/FormField/StringField.vue";
	import NumberField from "@/components/FormField/NumberField.vue";
	import StructField from "@/components/FormField/StructField.vue";
	
	const props = defineProps({
		baseUrl: {
			type: String,
			default: () => ""
		},
		name: {
			type: String,
			default: () => ""
		},
		pidentifier: {
	  		type: String,
	  		default: () => ""
		},
		list: {
			type: Array,
			default: () => []
		}
	})
	
	const baseUrl=ref<string>('');
	const name=ref<string>('');
	const pidentifier=ref<string>('');
	const styles=ref();
	
	watch(props,() => {
			baseUrl.value=props.baseUrl;
			name.value=props.name;
			pidentifier.value=props.pidentifier;
			styles.value=props.list;
		},
	    { immediate: true, deep: true },
	)
	
	const isExpand=ref(false);
	const handleExpand=()=>{
		isExpand.value=!isExpand.value
	}
	const emits = defineEmits(['onchange'])
	const onchange=(e:any)=>{
		emits('onchange',e)
	}
</script>